#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>
using namespace std;
int gvar = 1;
/*
    测试内容：两个执行流死循环，新执行流20秒后退出并返回1，让主执行流接收
    死循环时输出线程名字，并让gvar++并打印
*/

void showThreadName(const string &str)
{
    cout << "ThreadName is " << str;
}

void *threadRoutine(void *arg)
{
    const char *name = (const char *)arg;
    int cnt = 15;
    while (true)
    {
        showThreadName(name);
        cout << gvar++ << endl;
        if (cnt == 0)
        {
            break;
        }
        cnt--;
        sleep(1);
    }
    return (void *)1;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void *)"Thread 1 ");
    void *ret;

    int cnt = 10;
    while (true)
    {
        showThreadName("Main Thread ");
        gvar++;
        cout << gvar << endl;
        if(cnt == 0)
        {
            break;
        }
        cnt--;
        sleep(1);
    }
    int flag = pthread_join(tid, &ret);
    cout << "thread return value is " << (long long int)ret << endl;
    return 0;
}